package hunterxhunter.greedisland.data.map;

import hunterxhunter.greedisland.data.enums.Collision;
import java.awt.Rectangle;
import java.io.Serializable;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class Tile implements Serializable {

    private static transient Tile defaultTile = new Tile("/greedisland/img/map/entrance.png", new Rectangle(0, 0, 32, 32), Collision.NO);
    public static Tile getDefault() {
        return defaultTile;
    }
    
    private Layer[] layers;
    private Collision collision = Collision.NO;

    public Tile() {
        layers = new Layer[3];
        layers[LayerType.BACKGROUND.ordinal()]    = new Layer(null, null);
        layers[LayerType.FOREGROUND.ordinal()]    = new Layer(null, null);
        layers[LayerType.ALWAYS_ON_TOP.ordinal()] = new Layer(null, null);
    }
    
    public Tile(String backgroundImageRef, Rectangle backgroundSrc, Collision coll) {
        this();
        setImageRef(backgroundImageRef, LayerType.BACKGROUND);
        setSrc(backgroundSrc, LayerType.BACKGROUND);
        setCollision(collision);
    }
    
    public boolean hasLayerType(LayerType type) {
        return layers[type.ordinal()].isUsed();
    }
    
    public String getImageRef(LayerType type) {
        return layers[type.ordinal()].getImageRef();
    }
    
    public Rectangle getSrc(LayerType type) {
        return layers[type.ordinal()].getSrc();
    }
    
    public final void setImageRef(String imgRef, LayerType type) {
        layers[type.ordinal()].setImageRef(imgRef);
    }
    
    public final void setSrc(Rectangle src, LayerType type) {
        layers[type.ordinal()].setSrc(src);
    }
    
    
    // COLLISION
    
    public boolean isPassable() {
        return getCollision() == Collision.NO;
    }

    public void setPassable(boolean passable) {
        if(passable)
            setCollision(Collision.NO);
        else
            setCollision(Collision.ALL);
    }

    public Collision getCollision() {
        return collision;
    }

    public final void setCollision(Collision collision) {
        this.collision = collision;
    }
    
}
